//
//  RBCompeSectionTitleView.m
//  RMMaterialRed
//
//  Created by XBWH on 2024/3/12.
//

#import "RBCompeSectionTitleView.h"


@interface RBCompeSectionTitleView ()<JXCategoryViewDelegate>
@property(nonatomic,strong)UIView *bgView;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)JXCategoryTitleView *categoryView;
@end

@implementation RBCompeSectionTitleView
- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self rb_initViewUI];
        [self setNeedsLayout];
    }
    return self;
}
-(void)rb_initViewUI{
    self.backgroundColor = [UIColor clearColor];
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.titleLabel];
    [self.bgView addSubview:self.categoryView];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(RBScaleWidth(10));
        make.centerY.mas_equalTo(0);
    }];
    [self.categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(RBScaleWidth(10));
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}
-(void)setTitleArray:(NSArray *)titleArray{
    _titleArray=titleArray;
    if (_section==0) {
        if (!RBArrayIsEmpty(self.hotRecomArray)) {
            self.titleLabel.hidden = NO;
        }else{
            self.titleLabel.hidden = YES;
        }
        self.categoryView.hidden = YES;
    } else {
        self.titleLabel.hidden = YES;
        self.categoryView.hidden = NO;
    }
    self.categoryView.titles = titleArray;
    self.categoryView.defaultSelectedIndex = self.selectCurrentTag;
    [self.categoryView reloadData];
}
//是否可以选中item
-(BOOL)categoryView:(JXCategoryBaseView *)categoryView canClickItemAtIndex:(NSInteger)index{
    if (index==4) {
        //未登录
        if (![RBArchiveTool isLogin]) {
            [[RBOneKeyLogin shareInstance] rb_startLoginWithControllerType:RBOneKeyLoginVcTypeTopVc tabBarItemSelectIndex:2 otherSource:nil];
            return NO;
        }
    }
    return YES;
}
- (void)categoryView:(JXCategoryBaseView *)categoryView didClickSelectedItemAtIndex:(NSInteger)index{
    if(self.didClickSelectedItemAtIndexBlock){
        self.didClickSelectedItemAtIndexBlock(_section, index);
    }
}
- (UIView *)bgView{
    if(_bgView==nil){
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = RBColor(245, 245, 245);
    }
    return _bgView;
}
-(UILabel *)titleLabel{
    if(_titleLabel==nil){
        _titleLabel = [UILabel rb_labelWithTitle:@"热门推荐" color:RBColor(51, 51, 51) font:RBPingFangMediumFont(16) alignment:NSTextAlignmentLeft];
    }
    return _titleLabel;
}
- (JXCategoryTitleView *)categoryView{
    if(_categoryView==nil){
        _categoryView = [[JXCategoryTitleView alloc] init];
        _categoryView.delegate = self;
        _categoryView.contentEdgeInsetLeft = 0;
        _categoryView.cellSpacing = RBScaleWidth(30);
        _categoryView.titleFont = RBPingFangMediumFont(15);
        _categoryView.titleSelectedFont = RBPingFangMediumFont(16);
        _categoryView.titleColor = RBColor(153, 153, 153);
        _categoryView.titleSelectedColor = RBColor(51, 51, 51);
        JXCategoryIndicatorImageView *indicatorImageView = [[JXCategoryIndicatorImageView alloc] init];
        indicatorImageView.indicatorImageViewRollEnabled = NO;
        indicatorImageView.indicatorImageViewSize = CGSizeMake(31, 5);
        indicatorImageView.indicatorImageView.image = [UIImage imageNamed:@"hkk"];
        indicatorImageView.indicatorImageViewY = -10;
        _categoryView.indicators = @[indicatorImageView];
        [_categoryView reloadData];
    }
    return _categoryView;
}
@end
